/*
 * Copyright 2011 Stefan Ferstl
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.debmavenplugin.archiver.ar.deb;

import java.io.File;

import com.google.code.debmavenplugin.archiver.AbstractCollectingFileFilter;
import com.google.code.debmavenplugin.archiver.ArchiveMetaData;
import com.google.code.debmavenplugin.archiver.SpecialFileArchiveMetaData;
import com.google.code.debmavenplugin.archiver.ar.ArArchiver;
import com.google.code.debmavenplugin.archiver.ar.FileArArchiveEntry;


/**
 * A specialized {@link ArArchiver} that creates Debian packages.
 */
public class DebPackageArchiver extends ArArchiver {

  /**
   * Constructor.
   *
   * @param packageContentDirectory Directory where the Debian package's content
   * is stored. The Debian package will be created in this directory as well.
   * @param packageName The name of the Debian package.
   */
  public DebPackageArchiver(File packageContentDirectory, String packageName) {
    super(new File(packageContentDirectory, packageName), createDebPackageMetaData(packageContentDirectory), false);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected AbstractCollectingFileFilter<FileArArchiveEntry> createFileFilter(ArchiveMetaData metaData) {
    return new DebianPackageCollectingFileFilter(metaData);
  }

  /**
   * Helper method to create the metadata for the Debian package to be created.
   * @param baseDirectory Base directory of the package.
   * @return The created metadata.
   */
  private static ArchiveMetaData createDebPackageMetaData(File baseDirectory) {
    ArchiveMetaData debPackageMetaData = new SpecialFileArchiveMetaData(baseDirectory, null, null, null, 0644);
    debPackageMetaData.addSpecialFile( DebPackageConstants.DEB_BINARY_FILE, DebPackageConstants.ROOT,
        DebPackageConstants.ROOT, DebPackageConstants.FILE_MODE);
    debPackageMetaData.addSpecialFile(DebPackageConstants.CONTROL_ARCHIVE, null, null, DebPackageConstants.FILE_MODE);
    debPackageMetaData.addSpecialFile(DebPackageConstants.DATA_ARCHIVE, DebPackageConstants.ROOT,
        DebPackageConstants.ROOT, DebPackageConstants.FILE_MODE);

    return debPackageMetaData;
  }
}
